//
//  UpdateInfoController.m
//  Job
//
//  Created by 如此的你 on 2018/8/21.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "UpdateInfoController.h"
#import "JHPickerView.h"
@interface UpdateInfoController ()<UITextFieldDelegate>

@property (nonatomic , strong) UIBarButtonItem *rightItem;
@property (nonatomic , strong) UITextField *theNameTF;
@property(nonatomic , strong) NSMutableArray *dataSource;

@end

@implementation UpdateInfoController

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIView *contentView = [UIView new];
    contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentView];
    
    self.theNameTF = [[UITextField alloc]init];
    self.theNameTF.textColor = HexRGB(0x444444);
    self.theNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.theNameTF.text = self.text;
    self.theNameTF.delegate = self;
    self.theNameTF.placeholder = self.placheholder;
    [self.theNameTF addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];
    [contentView addSubview:self.theNameTF];
    
    
    
    UIView *view = [UIView new];
    view.backgroundColor = HexRGB(0xeeeeee);
    [contentView addSubview:view];
    
    UIView *view1 = [UIView new];
    view1.backgroundColor = HexRGB(0xeeeeee);
    [contentView addSubview:view1];
    
    self.rightItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(updateInfo)];
    self.rightItem.enabled = NO;
    self.navigationItem.rightBarButtonItem = self.rightItem;
    

    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(10);
        make.height.mas_offset(@50);
        make.right.left.equalTo(self.view);
    }];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(contentView);
        make.height.mas_offset(@1);
    }];
    
    [self.theNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(10);
        make.top.equalTo(view1.mas_bottom);
        make.right.equalTo(contentView).offset(-10);
        make.bottom.equalTo(view.mas_top);
    }];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(contentView);
        make.height.mas_offset(@1);
    }];
    
}

- (void)updateInfo {
    XLUserModel *user = [XLUserModel getUserInfo];
    [user setValue:self.theNameTF.text forKey:self.key];
    
    
    [XLUserModel saveUserInfo:user];
    [self.navigationController popViewControllerAnimated:YES];
//    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[auth mj_JSONObject]];
//    dict[@"userId"] = auth.userID;
//
//    NSString *url = @"/api//v4/user/resume";
//    [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
//        if (!error) {
//            if (!msg) {
//
//
//
//
//
//            } else {
//                [MBProgressHUD showError:msg];
//            }
//        }
//    }];
}

-(void)changeValue:(UITextField *)textField{
    NSString *value = textField.text;
    if ([value isEqualToString:self.text] || [value isEqualToString:@""] || value.length <= 0) {
        self.rightItem.enabled = NO;
    }else{
        self.rightItem.enabled = YES;
    }
}

/** textField */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    WeakSelf(weakSelf);
    if ([self.key isEqualToString:@"age"]) {
        for (int i=18; i<60; i++) {
            [self.dataSource addObject:[NSString stringWithFormat:@"%d", i]];
        }
        JHPickerView *pickerView = [JHPickerView show:self.dataSource];
        pickerView.selectBlock = ^(NSInteger index, NSString *data) {
            weakSelf.rightItem.enabled = YES;
            textField.text = data;
        };
        return NO;
    }
    
    if ([self.key isEqualToString:@"height"]) {
        for (int i=150; i<230; i++) {
            [self.dataSource addObject:[NSString stringWithFormat:@"%d", i]];
        }
        JHPickerView *pickerView = [JHPickerView show:self.dataSource];
        pickerView.selectBlock = ^(NSInteger index, NSString *data) {
            weakSelf.rightItem.enabled = YES;
            textField.text = data;
        };
        return NO;
    }
    
    if ([self.key isEqualToString:@"identity"]) {
        self.dataSource = [NSMutableArray arrayWithObjects:@"学生", @"社会人员", nil];
        JHPickerView *pickerView = [JHPickerView show:self.dataSource];
        pickerView.selectBlock = ^(NSInteger index, NSString *data) {
            weakSelf.rightItem.enabled = YES;
            textField.text = data;
        };
        return NO;
    }
    
    if ([self.key isEqualToString:@"education"]) {
        self.dataSource = [NSMutableArray arrayWithObjects:@"高中", @"专科", @"本科及以上",nil];
        JHPickerView *pickerView = [JHPickerView show:self.dataSource];
        pickerView.selectBlock = ^(NSInteger index, NSString *data) {
            weakSelf.rightItem.enabled = YES;
            textField.text = data;
        };
        return NO;
    }
    
    if ([self.key isEqualToString:@"enrollmentYear"]) {
        for (int i=1990; i<2019; i++) {
            [self.dataSource addObject:[NSString stringWithFormat:@"%d", i]];
        }
        JHPickerView *pickerView = [JHPickerView show:self.dataSource];
        pickerView.selectBlock = ^(NSInteger index, NSString *data) {
            weakSelf.rightItem.enabled = YES;
            textField.text = data;
        };
        return NO;
    }
    
    if ([self.key isEqualToString:@"gender"]) {
        self.dataSource = [NSMutableArray arrayWithObjects:@"男", @"女", nil];
        JHPickerView *pickerView = [JHPickerView show:self.dataSource];
        pickerView.selectBlock = ^(NSInteger index, NSString *data) {
            weakSelf.rightItem.enabled = YES;
            textField.text = data;
        };
        return NO;
    }
    
    return YES;
}






@end
